package com.tutu.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tutu.commons.pojo.Address;
import com.tutu.commons.utils.R;
import com.tutu.user.mapper.AddressMapper;
import com.tutu.user.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @Program: b2c-cloud-store
 * @Description:
 * @Author: JK.TuTu
 * @ @Version: 1.0
 * @Create: 2022-12-14 11:05
 **/
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    /**
     * 通过用户id查询地址信息
     * @param userId 用户id
     * @return 返回结果实体对象
     */
    @Override
    public List<Address> getAddressById(Integer userId) {
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Address::getUserId,userId);
        List<Address> addrList = addressMapper.selectList(queryWrapper);
        return addrList;
    }

    /**
     * 添加地址信息
     * @param address 添加的地址信息
     * @return  返回结果实体对象
     */
    @Override
    public R saveAddress(Address address) {
        int insert = addressMapper.insert(address);
        if(insert==0){
            return R.fail("地址添加失败!");
        }
        List<Address> addressList = getAddressById(address.getUserId());
        return R.ok("添加成功",addressList);
    }


    /**
     * 删除地址信息
     * @param id 地址信息id
     * @return 删除的行数
     */
    @Override
    public int remove(Integer id) {
        return addressMapper.deleteById(id);
    }
}
